package component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import pojo.TestBean;
import pojo.beforeinstantiation.MyBeforeInstantiation;

import java.lang.reflect.Constructor;

/**
 * @author: ZhouBert
 * @date: 2020/9/22
 * @description:	测试 自定义 SmartInstantiationAwareBeanPostProcessor
 */
public class MySmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {

	@Override
	public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
		// if 传入的 bean name == MyBeforeInstantiation -> 指定执行有参构造函数
		if(beanClass.getSimpleName().equals(MyBeforeInstantiation.class.getSimpleName())){
			try {
				System.out.println("准备执行 MyBeforeInstantiation 有参构造方法！");
				return new Constructor[]{beanClass.getDeclaredConstructor(TestBean.class)};
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
